package com.ruoyi.channel.service.impl;

import java.util.List;

import com.ruoyi.channel.domain.ChannelWithdrawalBank;
import com.ruoyi.channel.mapper.ChannelWithdrawalBankMapper;
import com.ruoyi.channel.service.IChannelWithdrawalBankService;
import com.ruoyi.common.core.domain.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *  服务层实现
 * 
 * @author ruoyi
 * @date 2019-05-08
 */
@Service
public class ChannelWithdrawalBankServiceImpl implements IChannelWithdrawalBankService
{
	Logger logger = LoggerFactory.getLogger(ChannelWithdrawalBankServiceImpl.class);

	@Autowired
	private ChannelWithdrawalBankMapper channelWithdrawalBankMapper;

	/**
     * 查询信息
     * 
     * @param id ID
     * @return 信息
     */
    @Override
	public ChannelWithdrawalBank selectChannelWithdrawalBankById(Long id)
	{
	    return channelWithdrawalBankMapper.selectChannelWithdrawalBankById(id);
	}
	
	/**
     * 查询列表
     * 
     * @param channelWithdrawalBank 信息
     * @return 集合
     */
	@Override
	public List<ChannelWithdrawalBank> selectChannelWithdrawalBankList(ChannelWithdrawalBank channelWithdrawalBank)
	{
	    return channelWithdrawalBankMapper.selectChannelWithdrawalBankList(channelWithdrawalBank);
	}
	
    /**
     * 新增
     * 
     * @param channelWithdrawalBank 信息
     * @return 结果
     */
	@Override
	public int insertChannelWithdrawalBank(ChannelWithdrawalBank channelWithdrawalBank)
	{
	    return channelWithdrawalBankMapper.insertChannelWithdrawalBank(channelWithdrawalBank);
	}
	
	/**
     * 修改
     * 
     * @param channelWithdrawalBank 信息
     * @return 结果
     */
	@Override
	public int updateChannelWithdrawalBank(ChannelWithdrawalBank channelWithdrawalBank)
	{
	    return channelWithdrawalBankMapper.updateChannelWithdrawalBank(channelWithdrawalBank);
	}

	/**
     * 删除对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteChannelWithdrawalBankByIds(String ids)
	{
		return channelWithdrawalBankMapper.deleteChannelWithdrawalBankByIds(Convert.toStrArray(ids));
	}



	/**
	 * 根据渠道id 查询已添加银行
	 *
	 * @param channelId 渠道id
	 * @return 渠道-代付银行集合
	 */
	@Override
	public List<ChannelWithdrawalBank> selectChannelWithdrawalBankListByChannelId(Integer channelId) {
		return channelWithdrawalBankMapper.selectChannelWithdrawalBankListByChannelId(channelId);
	}



	/**
	 * 添加渠道-代付银行
	 *
	 * @param channelId 渠道id
	 * @param bankIds   银行id数组
	 * @return 			结果
	 */
	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
	public AjaxResult insertChannelWithdrawalBankByChannelIdAndBankIds(Long channelId, Long[] bankIds) {
		try {
			if (bankIds.length > 0) {
				for (int i = 0; i < bankIds.length; i++) {
					Long bankId = bankIds[i];
					ChannelWithdrawalBank channelWithdrawalBank = new ChannelWithdrawalBank();
					channelWithdrawalBank.setChannelId(channelId);
					channelWithdrawalBank.setBankId(bankId);
					channelWithdrawalBankMapper.insertChannelWithdrawalBank(channelWithdrawalBank);
				}
				return AjaxResult.success("添加成功！");
			}else {
				return AjaxResult.error("添加失败");
			}
		} catch (Exception e) {
			logger.error("添加渠道-代付银行异常！请联系管理员", e);
			return AjaxResult.error("添加渠道-代付银行异常！请联系管理员",e);
		}
	}



}
